# ============================================================================
# Table 7: Effect of source name among subgroups for policing policy
# ============================================================================

# --- White respondents ---
data_pol_white <- filter(.data = data, police == 1 & white == 1)
diff_white <- mean(data_pol_white$support_policy[data_pol_white$deandre == 1]) -
  mean(data_pol_white$support_policy[data_pol_white$deandre == 0])
mean_white <- mean(data$support_policy[data$deandre == 0 & data$police == 1 & data$white == 1])
sd_white <- sqrt(mean((data_pol_white$support_policy[data_pol_white$deandre == 0] - mean_white)^2))
N_white <- nrow(data_pol_white)

n_deandre_white <- sum(data_pol_white$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_white <- obtain_permutation_matrix(declaration = declare_ra(N = N_white,
                                                                  m = n_deandre_white),
                                         maximum_permutations = n_sims)
null_white <- sapply(X = 1:ncol(Omega_white),
                     FUN = function(x) {
                       mean(data_pol_white$support_policy[Omega_white[,x] == 1]) -
                         mean(data_pol_white$support_policy[Omega_white[,x] == 0])})
pval_white <- min(1, 2 * min(mean(null_white <= diff_white), mean(null_white >= diff_white)))

# --- Nonwhite respondents ---
data_pol_nonwhite <- filter(.data = data, police == 1 & white == 0)
diff_nonwhite <- mean(data_pol_nonwhite$support_policy[data_pol_nonwhite$deandre == 1]) -
  mean(data_pol_nonwhite$support_policy[data_pol_nonwhite$deandre == 0])
mean_nonwhite <- mean(data_pol_nonwhite$support_policy[data_pol_nonwhite$deandre == 0])
sd_nonwhite <- sqrt(mean((data_pol_nonwhite$support_policy[data_pol_nonwhite$deandre == 0] - mean_nonwhite)^2))
N_nonwhite <- nrow(data_pol_nonwhite)

n_deandre_nonwhite <- sum(data_pol_nonwhite$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_nonwhite <- obtain_permutation_matrix(declaration = declare_ra(N = N_nonwhite,
                                                                     m = n_deandre_nonwhite),
                                            maximum_permutations = n_sims)
null_nonwhite <- sapply(X = 1:ncol(Omega_nonwhite),
                        FUN = function(x) {
                          mean(data_pol_nonwhite$support_policy[Omega_nonwhite[,x] == 1]) -
                            mean(data_pol_nonwhite$support_policy[Omega_nonwhite[,x] == 0])})
pval_nonwhite <- min(1, 2 * min(mean(null_nonwhite <= diff_nonwhite), mean(null_nonwhite >= diff_nonwhite)))

# --- GOP respondents ---
data_pol_gop <- filter(.data = data, police == 1 & republican == 1)
diff_gop <- mean(data_pol_gop$support_policy[data_pol_gop$deandre == 1]) -
  mean(data_pol_gop$support_policy[data_pol_gop$deandre == 0])
mean_gop <- mean(data_pol_gop$support_policy[data_pol_gop$deandre == 0])
sd_gop <- sqrt(mean((data_pol_gop$support_policy[data_pol_gop$deandre == 0] - mean_gop)^2))
N_gop <- nrow(data_pol_gop)

n_deandre_gop <- sum(data_pol_gop$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_gop <- obtain_permutation_matrix(declaration = declare_ra(N = N_gop,
                                                                m = n_deandre_gop),
                                       maximum_permutations = n_sims)
null_gop <- sapply(X = 1:ncol(Omega_gop),
                   FUN = function(x) {
                     mean(data_pol_gop$support_policy[Omega_gop[,x] == 1]) -
                       mean(data_pol_gop$support_policy[Omega_gop[,x] == 0])})
pval_gop <- min(1, 2 * min(mean(null_gop <= diff_gop), mean(null_gop >= diff_gop)))

# --- Non-GOP respondents ---
data_pol_nongop <- filter(.data = data, police == 1 & republican == 0)
diff_nongop <- mean(data_pol_nongop$support_policy[data_pol_nongop$deandre == 1]) -
  mean(data_pol_nongop$support_policy[data_pol_nongop$deandre == 0])
mean_nongop <- mean(data_pol_nongop$support_policy[data_pol_nongop$deandre == 0])
sd_nongop <- sqrt(mean((data_pol_nongop$support_policy[data_pol_nongop$deandre == 0] - mean_nongop)^2))
N_nongop <- nrow(data_pol_nongop)

n_deandre_nongop <- sum(data_pol_nongop$deandre)
set.seed(seed = 09291992, kind = "L'Ecuyer-CMRG")
Omega_nongop <- obtain_permutation_matrix(declaration = declare_ra(N = N_nongop,
                                                                   m = n_deandre_nongop),
                                          maximum_permutations = n_sims)
null_nongop <- sapply(X = 1:ncol(Omega_nongop),
                      FUN = function(x) {
                        mean(data_pol_nongop$support_policy[Omega_nongop[,x] == 1]) -
                          mean(data_pol_nongop$support_policy[Omega_nongop[,x] == 0])})
pval_nongop <- min(1, 2 * min(mean(null_nongop <= diff_nongop), mean(null_nongop >= diff_nongop)))

diff_white_str <- fmt(x = diff_white, digits = 4, pval = pval_white)
diff_nonwhite_str <- fmt(x = diff_nonwhite, digits = 4, pval = pval_nonwhite)
diff_gop_str <- fmt(x = diff_gop, digits = 4, pval = pval_gop)
diff_nongop_str <- fmt(x = diff_nongop, digits = 4, pval = pval_nongop)

mean_white_str <- fmt(x = mean_white, digits = 4)
mean_nonwhite_str <- fmt(x = mean_nonwhite, digits = 4)
mean_gop_str <- fmt(x = mean_gop, digits = 4)
mean_nongop_str <- fmt(x = mean_nongop, digits = 4)

sd_white_str <- fmt(x = sd_white, digits = 4)
sd_nonwhite_str <- fmt(x = sd_nonwhite, digits = 4)
sd_gop_str <- fmt(x = sd_gop, digits = 4)
sd_nongop_str <- fmt(x = sd_nongop, digits = 4)

pval_white_str <- fmt(x = pval_white, digits = 4)
pval_nonwhite_str <- fmt(x = pval_nonwhite, digits = 4)
pval_gop_str <- fmt(x = pval_gop, digits = 4)
pval_nongop_str <- fmt(x = pval_nongop, digits = 4)

N_white_str <- format_int(x = N_white)
N_nonwhite_str <- format_int(x = N_nonwhite)
N_gop_str <- format_int(x = N_gop)
N_nongop_str <- format_int(x = N_nongop)

table_7 <- sprintf(fmt = "\\caption{Estimated effects of Black source on support for race-conscious policy by race and party of respondents}
\\centering 
\\begin{tabular}{@{\\extracolsep{5pt}}lcccc} 
\\\\[-1.8ex]\\hline 
\\hline \\\\[-1.8ex] 
 & \\multicolumn{4}{c}{\\textit{Dependent variable:}} \\\\ 
\\cline{2-5} 
\\\\[-1.8ex] & \\multicolumn{4}{c}{Race-conscious Policy Support}  \\\\ 
\\\\[-1.8ex] & White & non-White & GOP & non-GOP \\\\ 
\\hline \\\\[-1.8ex] 
Black Source Cue & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
White source cue (control) mean & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
White source cue (control) SD & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
RI $p$-value (two-sided) & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
Number of observations & $%s$ & $%s$ & $%s$ & $%s$ \\\\ 
\\hline 
\\hline 
\\textit{Note:} & \\multicolumn{4}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\\\ 
\\end{tabular}",
                   diff_white_str,
                   diff_nonwhite_str,
                   diff_gop_str,
                   diff_nongop_str,
                   mean_white_str,
                   mean_nonwhite_str,
                   mean_gop_str,
                   mean_nongop_str,
                   sd_white_str,
                   sd_nonwhite_str,
                   sd_gop_str,
                   sd_nongop_str,
                   pval_white_str,
                   pval_nonwhite_str,
                   pval_gop_str,
                   pval_nongop_str,
                   N_white_str,
                   N_nonwhite_str,
                   N_gop_str,
                   N_nongop_str)

# Write LaTeX to file
writeLines(text = table_7, con = "code_and_output/tables/table_7.tex")

rm(list = setdiff(ls(), c("data", "fmt", "format_int", "n_sims")))
gc()